#pragma once

#include <string>

using namespace std;
/* 二叉树类
  2019年8月29日 15:33:54 */
template <class T>
class BinaryTree
{
private:
	/* 泛型数据 */
	T data;
	/* 指向左子树的指针*/
	BinaryTree<T>* left;
	/* 指向右子树的指针*/
	BinaryTree<T>* right;

public:
	~BinaryTree()
	{

	}
	void setData(T data);
	T getData(void);
	/* 将数据转成可打印的字符串 */
	string getPrintData(void);
	/* 使用数组构建二叉树 */
	BinaryTree<T> createBinTree(T arr[], int& index);
	/* 前序遍历二叉树*/
	void preOrderPrint(BinaryTree<T>* child);

};

template <class T>
void BinaryTree<T>::setData(T data) {
	this->data = data;
}

template <class T>
T BinaryTree<T>::getData() {
	return this->data;
}

template <class T>
string BinaryTree<T>::getPrintData() {
	if (typeid(this->data) == typeid(int)) {
		return to_string(this->data);
	}
	else if (typeid(this->data) == typeid(string)) {
		return this->data;
	}
	else if (typeid(this->data) == typeid(char)) {
		return to_string(this->data);
	}
	else {
		return to_string(this->data);
	}
}

template <class T>
BinaryTree<T> BinaryTree<T>::createBinTree(T arr[], int& index) {
	T data = arr[index];
	if (data == NULL) {
		return NULL;
	}
	else {
		BinaryTree<T>* binTree = new BinaryTree<T>();
		binTree->data = data;
		binTree->left = binTree->createBinTree(arr, index);
		binTree->right = binTree->createBinTree(arr, index);
	}
}

template <class T>
void BinaryTree<T>::preOrderPrint(BinaryTree<T>* child) {
	if (child == NULL) {
		return;
	}
	else
	{
		cout << child->getData() << endl;
		this->preOrderPrint(child->left);
		this->preOrderPrint(child->right);
	}
}